package learning.socket5;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Client {
	public static void main(String[] args) {
		Socket s = null;
		try {
			s = new Socket("localhost", 5200);
			BufferedReader br = new BufferedReader(new InputStreamReader(
					System.in));
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
					s.getOutputStream()));
			System.out.println("please enter your name!");
			while (true) {  //tips;thread should be created again and again
				new Rec_MsgThread(s).start();
				String str_sent=br.readLine();
				bw.write(str_sent);
				bw.newLine();
				bw.flush();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

class Rec_MsgThread extends Thread {
	Socket s = null;
	SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss");
	public Rec_MsgThread(Socket s) {
		super();
		this.s = s;
	}

	@Override
	public void run() {
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(
					s.getInputStream()));
			System.out.println(br.readLine()+"  "+sdf.format(new Date()));

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
